/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.buzz.registwork.service;

import java.util.List;

import com.jeeplus.modules.adjunctFile.entity.AdjunctFile;
import com.jeeplus.modules.adjunctFile.mapper.AdjunctFileMapper;
import com.jeeplus.modules.buzz.registwork.entity.Registstudyentry;
import com.jeeplus.modules.buzz.registwork.mapper.RegiststudyentryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.buzz.registwork.entity.Registwork;
import com.jeeplus.modules.buzz.registwork.mapper.RegistworkMapper;
import com.jeeplus.modules.buzz.registwork.entity.Registworkentry;
import com.jeeplus.modules.buzz.registwork.mapper.RegistworkentryMapper;

/**
 * 作业登记Service
 * @author 陈
 * @version 2025-07-14
 */
@Service
@Transactional(readOnly = true)
public class RegistworkService extends CrudService<RegistworkMapper, Registwork> {

	@Autowired
	private RegistworkentryMapper registworkentryMapper;

	@Autowired
	private RegiststudyentryMapper registstudyentryMapper;

	@Autowired
	private AdjunctFileMapper adjunctFileMapper;


	public Registwork get(String id) {
		Registwork registwork = super.get(id);
		registwork.setRegistworkentryList(registworkentryMapper.findList(new Registworkentry(registwork)));
		registwork.setRegiststudyentryList(registstudyentryMapper.findList(new Registstudyentry(registwork)));
		registwork.setAdjunctFileList(adjunctFileMapper.findList(new AdjunctFile("作业登记",registwork.getId())));

		return registwork;
	}

	public List<Registwork> findList(Registwork registwork) {
		return super.findList(registwork);
	}

	public Page<Registwork> findPage(Page<Registwork> page, Registwork registwork) {
		return super.findPage(page, registwork);
	}

	@Transactional(readOnly = false)
	public void save(Registwork registwork) {
		super.save(registwork);
		for (Registworkentry registworkentry : registwork.getRegistworkentryList()){
			if (registworkentry.getId() == null){
				continue;
			}
			if (Registworkentry.DEL_FLAG_NORMAL.equals(registworkentry.getDelFlag())){
				if (StringUtils.isBlank(registworkentry.getId())){
					registworkentry.setInteryid(registwork);
					registworkentry.preInsert();
					registworkentryMapper.insert(registworkentry);
				}else{
					registworkentry.preUpdate();
					registworkentryMapper.update(registworkentry);
				}
			}else{
				registworkentryMapper.delete(registworkentry);
			}
		}
		for (Registstudyentry registstudyentry : registwork.getRegiststudyentryList()){
			if (registstudyentry.getId() == null){
				continue;
			}
			if (Registstudyentry.DEL_FLAG_NORMAL.equals(registstudyentry.getDelFlag())){
				if (StringUtils.isBlank(registstudyentry.getId())){
					registstudyentry.setInteryid(registwork);
					registstudyentry.preInsert();
					registstudyentryMapper.insert(registstudyentry);
				}else{
					registstudyentry.preUpdate();
					registstudyentryMapper.update(registstudyentry);
				}
			}else{
				registstudyentryMapper.delete(registstudyentry);
			}
		}
		for (AdjunctFile adjunctFile : registwork.getAdjunctFileList()){
			if (StringUtils.isBlank(adjunctFile.getId())){
				adjunctFile.setObjectId("作业登记");
				adjunctFile.setDataId(registwork.getId());
				adjunctFile.preInsert();
				adjunctFileMapper.insert(adjunctFile);
			}else{
				adjunctFile.preUpdate();
				adjunctFileMapper.update(adjunctFile);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(Registwork registwork) {
		super.delete(registwork);
		registworkentryMapper.delete(new Registworkentry(registwork));
		registstudyentryMapper.delete(new Registstudyentry(registwork));
		adjunctFileMapper.delete(new AdjunctFile("作业登记",registwork.getId()));
	}

}
